var config = require('../../config.js');
var util = require('../utils/util.js');
function load_page(data, currentPage, tab_tab_list, page){
  wx.request({
    url: config.series +'/search/findByTagName',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var templist = tab_tab_list.list;
      var series = data._embedded.series;
      //接口获取 收藏
      templist = templist.concat(series);
      tab_tab_list.list = templist;
      tab_tab_list.page = currentPage;

      page.setData({
        list: templist
      });
    }
  });
}

function load_list(data, currentPage, tab_tab_list, page){
  var list=wx.getStorageSync("series_new");
  if(list){
    tab_tab_list = list;
    page.setData({
      list: list.list
    });
    return;
  }

  wx.request({
    url: config.series +'/search/findByTagName',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var series = data._embedded.series;
      //接口获取 收藏
      var templist = series;
      tab_tab_list.list = templist;
      tab_tab_list.page = currentPage;
      tab_tab_list.pageSize = data.page.totalPages;
      wx.setStorageSync("series_new", tab_tab_list);
      page.setData({
        list: templist
      });
    }
  });
}

function load_hot_list(data, currentPage, tab_tab_list, page) {
  var list = wx.getStorageSync("series_hot");
  if (list) {
    tab_tab_list = list;
    page.setData({
      list: list.list
    });
    return;
  }

  wx.request({
    url: config.series + '/search/findByTagName',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var series = data._embedded.series;
      //接口获取 收藏
      var templist = series;
      tab_tab_list.list = templist;
      tab_tab_list.page = currentPage;
      tab_tab_list.pageSize = data.page.totalPages;
      wx.setStorageSync("series_hot", tab_tab_list);
      page.setData({
        list: templist
      });
    }
  });
}

function load_status_page(data, currentPage, tab_tab_list, page) {
  wx.request({
    url: config.series + '/search/findByTagNameAndStatus',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var templist = tab_tab_list.list;
      var series = data._embedded.series;

      //接口获取 收藏
      templist = templist.concat(series);
      tab_tab_list.list = templist;
      tab_tab_list.page = currentPage;
      page.setData({
        list: templist
      });
    }
  });
}
function load_status_list(data, currentPage, tab_tab_list, page) {
  var list = wx.getStorageSync("series_status");
  if (list) {
    tab_tab_list = list;
    page.setData({
      list: list.list
    });
    return;
  }
  wx.request({
    url: config.series + '/search/findByTagNameAndStatus',
    data: data,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var series = data._embedded.series;
      //接口获取 收藏
      var templist = series;
      tab_tab_list.list = templist;
      tab_tab_list.page = currentPage;
      tab_tab_list.pageSize = data.page.totalPages;
      wx.setStorageSync("series_status",tab_tab_list);
      page.setData({
        list: templist
      });
    }
  });
}
//加载热词
function  load_hotword(keywords,tab_page,page){
  wx.request({
    url: config.search + '/terms?prefix=' + keywords,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
        var data=res.data;
        tab_page.hotwords_list = data.content;
        page.setData({
          hotwords_list:data.content
        });
    }
  });

}

//flag  区别默认进入或是选择  true false
function search_time(date,page,flag){
  if (!date){
    page.setData({
      list: []
    });
  }
  var date = new Date(date);
  var year = date.getFullYear();
  var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
  var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
  var keywords = year + '' + month + '' + day;
  wx.request({
    url: config.materialDocuments + '/search/findByTagNameAndKeywords',
    data: {
      tagName: config.tagName,
      keywords: keywords,
      page: 0,
      size: 20
    },
    header: {
      'content-type': 'application/json'
    }, success: function (res) {
      var materialDocuments = res.data._embedded.materialDocuments;
      var list=[];

      if (materialDocuments.length == 0 && !flag){
        wx.showModal({
          title: '错误提示',
          content: '请重新选择时间范围',
          showCancel:false,
          confirmText:'好的'
        });
      }

      for (var i = 0; i < materialDocuments.length; i++) {
        materialDocuments[i].showTime = util.formateDate(materialDocuments[i].showTime);
        materialDocuments[i].day = util.getDays(materialDocuments[i].showTime);
        if (materialDocuments[i].type == 'VIDEO') {
          materialDocuments[i].bindtap = 'inplay_material_p';

          var foramt = util.formateDate(materialDocuments[i].showTime);
          materialDocuments[i].showTime = foramt;
          materialDocuments[i].day = util.getDays(materialDocuments[i].showTime);


          var date = new Date(foramt);
          materialDocuments[i].hour = date.getFullYear();
          materialDocuments[i].minutes = (date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
          materialDocuments[i].seconds = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
          var lengthInSecond = materialDocuments[i].lengthInSecond;
          if (lengthInSecond) {
            materialDocuments[i].lengthInSecond = transSS(lengthInSecond);
          }
          list.push(materialDocuments[i]);
        } 
      }
      page.setData({
        list: list
      });
    }
  });    
}


module.exports = {
  load_page: load_page,
  load_status_page: load_status_page,
  load_list: load_list,
  load_hot_list: load_hot_list,
  load_status_list: load_status_list,
  load_hotword: load_hotword,
  search_time: search_time
}